Skip to content

Feat/two phase memory extraction#106

Open
yuanhechen wants to merge 5 commits intoEverMind-AI:mainfrom
leow3lab:feat/two-phase-memory-extraction
Open

Feat/two phase memory extraction#106
yuanhechen wants to merge 5 commits intoEverMind-AI:mainfrom
leow3lab:feat/two-phase-memory-extraction

Conversation

@yuanhechen
Copy link

Description

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test improvements
  • Build/CI/CD changes

Related Issues

Fixes #
Relates to #

Changes Made

Testing

  • Tested locally with manual verification
  • Added/updated unit tests
  • Added/updated integration tests
  • All existing tests pass

Test Configuration:

  • OS:
  • Python version:
  • Database versions (if relevant):

Test Results:

# Paste relevant test output here

Checklist

  • My code follows the project's code style guidelines
  • I have performed a self-review of my code
  • I have commented my code where necessary, particularly in complex areas
  • I have updated the documentation accordingly
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have used Gitmoji in my commit messages
  • Any dependent changes have been merged and published

Screenshots (if applicable)

Additional Notes

Breaking Changes


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

root added 5 commits March 2, 2026 17:51
将 MEMORY_LANGUAGE 由 en 改为 zh,使 EverMemOS 在提取
情节记忆、前瞻规划、事件日志等记忆时使用中文 prompt,
避免 LLM 输出英文导致记忆内容中英文混杂。
问题:DELETE /api/v1/memories 只对 MongoDB MemCell 做软删除,
未清理 Milvus 向量索引,导致清空记忆后向量召回仍然返回已删除内容。

根本原因:记忆存储是双写(MongoDB + Milvus),但删除只有 MongoDB
一侧,Milvus 侧向量始终存在,向量搜索完全感知不到软删除标记。

修复方案(在 delete_by_combined_criteria 末尾新增步骤 7):
- 新增私有方法 _delete_milvus_vectors(),使用 asyncio.gather
  并行删除三类 Milvus 向量索引:
  - EpisodicMemoryMilvusRepository.delete_by_filters(user_id, group_id)
  - ForesightMilvusRepository.delete_by_filters(user_id, group_id)
  - EventLogMilvusRepository.delete_by_filters(user_id, group_id)
- 若携带 event_id,额外精确删除:
  - episodic_repo.delete_by_event_id(event_id)
  - foresight_repo.delete_by_parent_id(event_id)
  - event_log_repo.delete_by_parent_id(event_id)
- Milvus 清理失败仅 warning 日志,不影响主流程返回成功
  (MongoDB 侧已成功是主要保证,向量清理失败不应回滚)
实时阶段(REALTIME_EVENT_LOG_ONLY=true):
  每条消息立即提取 event_log(原子事实不依赖多轮上下文)
  跳过 episodic memory 和 foresight 提取
  event_log 以 MemCell 为 parent 直接存储(parent_type=memcell)

批处理阶段(BatchEpisodeWorker):
  后台定时任务每 N 分钟扫描一次(默认 15 分钟)
  收集同一 group 的多个 MemCell 合并为完整上下文窗口
  调用 episode LLM 生成高质量叙事记忆
  处理完成后在 MemCell.extend 中标记 batch_ep_done=True

新文件:
  biz_layer/mem_batch_episode_worker.py — BatchEpisodeWorker 类
  core/lifespan/batch_episode_lifespan.py — 应用启动时自动注册 Worker

相关环境变量:
  REALTIME_EVENT_LOG_ONLY=true    启用两阶段模式
  BATCH_EPISODE_INTERVAL=15       Worker 运行间隔(分钟)
  BATCH_EPISODE_LOOKBACK=120      向前查询窗口(分钟)
  BATCH_EPISODE_MIN_CELLS=3       触发生成的最少 MemCell 数
为MemCell排序使用的fallback datetime由naive的datetime.min
改为timezone-aware的datetime.min.replace(tzinfo=timezone.utc),
避免与带时区的MemCell.timestamp比较时抛出
TypeError: can't compare offset-naive and offset-aware datetimes
- docs/advanced/TWO_PHASE_EXTRACTION.md: 完整使用指南,含背景分析、
  快速上手、参数配置、工作流程详解、与默认模式的对比表、已知限制
- docs/CHANGELOG.md: 新增 [Unreleased] 条目,说明本次变更内容
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant